package com.fjec.devops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.devops.entity.StatAllUserNum;
import com.fjec.devops.entity.StatOrderInfo;
import com.fjec.devops.mapper.StatOrderInfoMapper;
import com.fjec.devops.service.IStatOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.devops.view.AllUserNumVO;
import com.fjec.devops.view.OrderNumVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 017.统计-订单信息统计表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-09-18
 */
@Service
public class StatOrderInfoServiceImpl extends ServiceImpl<StatOrderInfoMapper, StatOrderInfo> implements IStatOrderInfoService {

    @Resource
    private StatOrderInfoMapper mapper;

    @Override
    public JsonResult statOrderData(Integer days) throws Exception {
        // 通过days计算开始日期和结束日期
        LocalDate endTime = DateUtil.nowLd();
        LocalDate startTime = DateUtil.ldMinus(endTime, ChronoUnit.DAYS, days);
        List<StatOrderInfo> entityList = mapper.statOrderData(startTime, endTime, 1);
        List<String> xList = new ArrayList<>();
        List<Integer> dNumList = new ArrayList<>();
        List<Integer> bNumList = new ArrayList<>();
        List<Integer> allNumList = new ArrayList<>();
        List<Float> dAmountList = new ArrayList<>();
        List<Float> bAmountList = new ArrayList<>();
        List<Float> allAmountList = new ArrayList<>();
        for (StatOrderInfo entity : entityList) {
            if (entity.getOrderType().intValue() == 1) {
                dNumList.add(entity.getOrderNum());
                dAmountList.add(entity.getOrderAmount() / 100.0F);
            } else {
                bNumList.add(entity.getOrderNum());
                bAmountList.add(entity.getOrderAmount() / 100.0F);
            }
        }
        entityList = mapper.statOrderData(startTime, endTime, 0);
        for (StatOrderInfo entity : entityList) {
            xList.add(DateUtil.format(entity.getStatDate(), "MM月dd日"));
            allNumList.add(entity.getOrderNum());
            allAmountList.add(entity.getOrderAmount() / 100.0F);
        }
        Map<String, Object> resultMap = new HashMap<>();
        // 整理订单数量统计曲线
        List<Map<String, Object>> dataList = new ArrayList<>();
        // 订单总数
        Map<String, Object> tmpMap = new HashMap<>();
        tmpMap.put("name", "订单总数");
        tmpMap.put("data", allNumList);
        dataList.add(tmpMap);
        // 零售订单数
        tmpMap = new HashMap<>();
        tmpMap.put("name", "零售订单数");
        tmpMap.put("data", dNumList);
        dataList.add(tmpMap);
        // 批量订单数
        tmpMap = new HashMap<>();
        tmpMap.put("name", "批量订单数");
        tmpMap.put("data", bNumList);
        dataList.add(tmpMap);
        resultMap.put("numList", dataList);

        // 整理订单金额统计曲线
        dataList = new ArrayList<>();
        // 订单总数
        tmpMap = new HashMap<>();
        tmpMap.put("name", "订单总额");
        tmpMap.put("data", allAmountList);
        dataList.add(tmpMap);
        // 零售订单数
        tmpMap = new HashMap<>();
        tmpMap.put("name", "零售订单总额");
        tmpMap.put("data", dAmountList);
        dataList.add(tmpMap);
        // 批量订单数
        tmpMap = new HashMap<>();
        tmpMap.put("name", "批量订单总额");
        tmpMap.put("data", bAmountList);
        dataList.add(tmpMap);
        resultMap.put("amountList", dataList);
        // 横轴
        resultMap.put("xlist", xList);
        return JsonResult.success()
                .data(resultMap)
                .build();
    }

    @Override
    public JsonResult statOrderNumFakeData() throws Exception {
        LocalDateTime now = LocalDateTime.now();
        int before = 365;
        double rate = 0;
        int counter = 0;
        for (int i = 0; i < before; i++) {
            StatOrderInfo entity = new StatOrderInfo();
            LocalDateTime dt = now.minusDays(before - i);
            rate = Math.random();
            if (dt.getDayOfWeek().equals("FRIDAY")) {
                rate = rate * 1.3;
            }
            if (dt.getDayOfWeek().equals("SATURDAY")) {
                rate = rate * 12.3;
            }
            if (dt.getDayOfWeek().equals("SUNDAY")) {
                rate = rate * 30.3;
            }
            counter = counter + (int) Math.floor(3 * rate * 128 * i);
            int marketCode = (int) Math.random() * 6;
            entity.setMarkCode(marketCode);
            int orderType = (int) Math.random() * 10;
            entity.setOrderType(orderType);
            entity.setOrderNum(counter);
            entity.setOrderAmount(counter * (long) ((Math.random() + 1) * 1.0));
            entity.setStatDate(dt.toLocalDate());
            entity.setUpdateTime(dt);
            entity.setCreateTime(dt);
            // 这样会不会数据库压力太大
            mapper.insert(entity);
        }
        return JsonResult.success().build();
    }

}
